var app = getApp();
Page({
	data:{
		long:0,
    lat:0,
    center_long:0,
    center_lat:0,
    markers:[],
    controls: [],
    circles: [],
    data_url: "",
    a:"1",
    b:"0",
    winWidth: 0,
    winHeight: 0,
    nearby_map_h:0,
    scale: 16,
    encryptedData:"",
    session_key:"",
    iv:""
  }, 
  controltap(e) {//地图缩放
    var that = this
    if(e.controlId == 1){
      this.data.scale == 18 ? this.data.scale:this.data.scale++
      this.setData({
        long: that.data.center_long,
        lat: that.data.center_lat,
        scale:that.data.scale
        })
    }else if(e.controlId == 2){
      this.data.scale == 0 ? this.data.scale : this.data.scale--
      this.setData({
        long: that.data.center_long,
        lat: that.data.center_lat,
        scale: that.data.scale
      })
    }
  },
  // delmarker(e) {//删除商圈的方法
  //   console.log(e)
  //   var that = this
  //   wx.request({
  //     url: that.data.data_url + "/GPD/deletem.do",
  //     data: {
  //       id: e.markerId
  //     },
  //     success(res) {
  //       console.log(res)
  //     }
  //   })
  // },
  regionchange(e){//获取中心点坐标
    if(e.type == 'end'){
      this.getCenterLocation()
    }
  }
  ,
  getCenterLocation:function(res){
    this.mapCtx = wx.createMapContext('map')
    var that = this
    this.mapCtx.getCenterLocation({
      success: function (res) {
        var long = res.longitude
        var lat = res.latitude
        wx.request({
          url: that.data.data_url + "/GPD/getMerchantByCoordinate.do",
          data:{
            longitude:long,
            latitude:lat,
            row:100
          },
          success(res){
            var map_markers = [];
            var map_circles = [];
            for(var i = 0;i<res.data.length;i++){
              map_markers[i] = {
                id: res.data[i].id,
                longitude: res.data[i].longitude,
                latitude: res.data[i].latitude,
                title: res.data[i].merchatname,
                width: "40",
                height: "56",
                iconPath: "/images/map_ic.png"
              }
              map_circles[i] = {
                id: res.data[i].id,
                longitude: res.data[i].longitude,
                latitude: res.data[i].latitude,
                color: res.data[i].color,
                fillColor: res.data[i].fillcolor,
                radius: res.data[i].radius,
                strokeWidth: 1
              }
            }
            that.setData({
              markers: map_markers,
              circles: map_circles
            })
          }
        })
      }
    })
  },
  markertap:function(e){
    wx.navigateTo({
      url: '/pages/circle_det/circle_det?id='+e.markerId,
    })
  },
	onLoad:function(options){
    var that = this;
    this.setData({
      data_url: app.globalData.data_url
    })
    wx.getLocation({
      type:"gcj02",
      success: function(res) {
      },
    })
    wx.getSystemInfo({//渲染各个按钮
      success: function(res) {
        var map_controls = [{
            id: 1,
            position: { left: res.windowWidth - 80, top: res.windowHeight - 195, width: 60, height: 75 },
            iconPath: "/images/map_add.png",
            clickable: true
        }, {
            id: 2,
            position: { left: res.windowWidth - 80, top: res.windowHeight - 120, width: 60, height: 75 },
            iconPath: "/images/map_down.png",
            clickable: true
          }, {
            id: 3,
            position: { left: res.windowWidth / 2 - 15, top: res.windowHeight / 2 - 54, width: 30, height: 54 },
            iconPath: "/images/ic_pinter.png",
            clickable: true
        }
          ]
        that.setData({
          winWidth:res.windowWidth,//获取屏幕宽度
          winHeight: res.windowHeight,//获取屏幕高度
          nearby_map_h: res.windowHeight,//设置屏幕高度
          controls:map_controls//设置按钮
        })
      }
    })
  },
	onReady:function(){
    var that = this;
    var k = 0;
    var nickname;
    var od;
    var latLong = [{ lat_i: 0, long_i: 0 }, { lat_i: 0, long_i: 0 }, { lat_i: 0, long_i: 0 }, { lat_i: 0, long_i: 0 }, { lat_i: 0, long_i: 0 }];
    setTimeout(function () { clearInterval(tim) }, 4010)
    var tim = setInterval(function () {
      k++;
      wx.getLocation({
        type: "gcj02",
        success: function (res) {
          latLong[k].lat_i = res.latitude,
          latLong[k].long_i = res.longitude
        }
      })
      if (k === 3) {
        // console.log(latLong)
        // wx.getUserInfo({
        //   success: function (res) {
        //     wx.request({
        //       url: "http://192.168.0.108:8080/GPD/insertDataTest.do",
        //       data:{
        //         raw_data:res
        //       }
        //     })
        //   },
        // })
        wx.login({
        })
                // console.log(that.data)
                // console.log(that.data.nickname)
                // console.log(that.data.a)
                // console.log(that.data.winHeight)
                // console.log(that.data.winWidth)
                // console.log(typeof(that.data.openid))
      }
    }, 1000)
      wx.getLocation({//获取当前坐标
        type: "gcj02",// 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
        success: function (res) {
          var long = res.longitude
          var lat = res.latitude
          that.setData({
            long: long,
            lat: lat
          }),
            wx.request({
            url: that.data.data_url + "/GPD/testGetData.do",
              data: {
                long: that.data.long,
                lat: that.data.lat
              },
              success: function (res) {
                // console.log(res)
              }
            })
        },
      })
      wx.showModal({
        title: '温馨提示',
        content: '为防止您走丢，请开启GPS定位功能，iPhone用户请无视^_^',
        showCancel: false,
        confirmText: '朕知道了',
        confirmColor: '#ea6644',
      })
	},
	onShow:function(){
    
	},
	onHide:function(){
		
	},
	onUnload:function(){
		
	},
	onPullDownRefresh:function(){
		
	},
	onReachBottom:function(){
		
	}
})		
// map.js
// Page({
//   data: {
//     markers: [{
//       iconPath: "/resources/others.png",
//       id: 0,
//       latitude: 23.099994,
//       longitude: 113.324520,
//       width: 50,
//       height: 50
//     }],
//     polyline: [{
//       points: [{
//         longitude: 113.3245211,
//         latitude: 23.10229
//       }, {
//         longitude: 113.324520,
//         latitude: 23.21229
//       }],
//       color: "#FF0000DD",
//       width: 2,
//       dottedLine: true
//     }],
//     controls: [{
//       id: 1,
//       iconPath: '../../images/map_add.png',
//       position: {
//         left: 0,
//         top: 300 - 50,
//         width: 50,
//         height: 50
//       },
//       clickable: true
//     }]
//   },
//   regionchange(e) {
//     console.log(e.type)
//   },
//   markertap(e) {
//     console.log(e.markerId)
//   },
//   controltap(e) {
//     console.log(e)
//   }
// })